package com.fand.platform.authority.service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Transactional;

import com.fand.dto.PaginationDto;
import com.fand.platform.authority.dao.RoleDao;
import com.fand.platform.authority.dto.RoleDto;
import com.fand.platform.authority.dto.RoleReOpAcDto;

/**
 * 角色服务
 *
 * @author yaoHT
 * 
 */
@Getter 
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class RoleDefaultService implements RoleService {
	private RoleDao roleDao;
	
	@Override
	@CacheEvict(value = RoleService.CACHE_NAME, allEntries = true)
	public void reload() {
	}

	@Override
	@Transactional
	public RoleDto saveRoleByIdOrCodeForRole(RoleDto saveRole) {
		String[] whereEqProperties = null;

//		if (StringUtils.isNotBlank(saveRole.getId())) {
//			whereEqProperties = new String[] { "id" };
//		} else {
//			whereEqProperties = new String[] { "code" };
//		}
	
		RoleDto oldRole = this.roleDao.selectRoleBy1ForMappedObject(RoleDto.class,
				saveRole, null, whereEqProperties);

		if (oldRole == null) {
			this.roleDao.insertRoleBy1(saveRole);

			return saveRole;
		}
		
		String[] updateProperties = com.fand.utils.BeanUtils.differProperties(saveRole, oldRole, "code", "name");

		if (ArrayUtils.isNotEmpty(updateProperties)) {
			this.roleDao.updateRoleBy1(saveRole, updateProperties, whereEqProperties);

			com.fand.utils.BeanUtils.copyProperties(saveRole, oldRole, updateProperties);
		}

		return oldRole;
	}

	@Override
	@Transactional
	public RoleDto saveRoleBy2ForRole(RoleDto saveRole) {
		String[] whereEqProperties = null;

//		if (StringUtils.isNotBlank(saveRole.getId())) {
//			whereEqProperties = new String[] { "id" };
//		} else {
//			whereEqProperties = new String[] { "code" };
//		}
	
		RoleDto oldRole = this.roleDao.selectRoleBy1ForMappedObject(RoleDto.class,
				saveRole, null, whereEqProperties);

		if (oldRole == null) {
			this.roleDao.insertRoleBy1(saveRole, new String[] {});

			return saveRole;
		}
		
		String[] updateProperties = com.fand.utils.BeanUtils.differProperties(saveRole, oldRole, "code", "name");

		if (ArrayUtils.isNotEmpty(updateProperties)) {
			this.roleDao.updateRoleBy1(saveRole, updateProperties, whereEqProperties);

			com.fand.utils.BeanUtils.copyProperties(saveRole, oldRole, updateProperties);
		}

		return oldRole;
	}

	@Override
	@Transactional
	public int deleteRoleBy1(RoleDto deleteRole) {
		return this.roleDao.deleteRoleBy1(deleteRole, new String[] {});
	}

	@Override
	@Transactional
	public int deleteRoleByIds(RoleDto deleteRole) {
		return this.roleDao.deleteRoleByIds(deleteRole);
	}

	@Override
	@Transactional
	public int setRoleBy1(RoleDto setRole) {
		return this.roleDao.updateRoleBy1(setRole, new String[] {}, new String[] {});
	}

	@Override
	@Transactional
	public int setRoleBy2(RoleDto setRole) {
		return this.roleDao.updateRoleBy2(setRole);
	}

	@Override
	public Integer readRoleByCodeNqIdForInteger(RoleDto readRole) {
		return this.roleDao.selectRoleByCodeNqIdForInteger(readRole);
	}

	@Override
	public Integer readRoleBy2ForInteger(RoleDto readRole) {
		return this.roleDao.selectRoleBy2ForObject(Integer.class, readRole, null);
	}

	@Override
	public List<Integer> readListRoleBy1(RoleDto readRole) {
		return this.roleDao.selectRoleBy1ForObjectList(Integer.class, readRole, null,
				new String[] {});
	}

	@Override
	public List<Integer> readListRoleBy2(RoleDto readRole) {
		return this.roleDao.selectRoleBy2ForObjectList(Integer.class, readRole, null);
	}

	@Override
	public RoleDto getRoleBy1(RoleDto getRole) {
		return this.roleDao.selectRoleBy1ForMappedObject(RoleDto.class, getRole, null, "id");
	}

	@Override
	public RoleDto getRoleById(RoleDto getRole) {
		return this.roleDao.selectRoleBy1ForMappedObject(RoleDto.class, getRole, null, "id");
	}

	@Override
	public List<RoleDto> findRoleBy1(RoleDto findRole) {
		return this.roleDao.selectRoleBy1ForMappedObjectList(RoleDto.class, findRole,
				new String[] {}, new String[] {});
	}

	@Override
	public List<RoleDto> findRoleBy2(RoleDto findRole) {
		return this.roleDao.selectRoleBy2ForMappedObjectList(RoleDto.class, findRole);
	}

	@Override
	public PaginationDto<RoleDto> paginationRoleBy1ForRoleList(RoleDto paginationRole) {
		return this.roleDao.paginationRoleBy1ForMappedObjectList(RoleDto.class,
				paginationRole);
	}

	@Override
	public PaginationDto<Map<String, Object>> paginationRoleBy1ForMapList(RoleDto paginationRole) {
		return this.roleDao.paginationRoleBy1ForMapList(paginationRole);
	}

	@Override
	@Transactional
	public RoleReOpAcDto saveRoleReOpAcByIdOrCodeForRoleReOpAc(RoleReOpAcDto saveRoleReOpAc) {
		String[] whereEqProperties = null;

//		if (StringUtils.isNotBlank(saveRoleReOpAc.getId())) {
//			whereEqProperties = new String[] { "id" };
//		} else {
//			whereEqProperties = new String[] { "code" };
//		}
	
		RoleReOpAcDto oldRoleReOpAc = this.roleDao.selectRoleReOpAcBy1ForMappedObject(RoleReOpAcDto.class,
				saveRoleReOpAc, null, whereEqProperties);

		if (oldRoleReOpAc == null) {
			this.roleDao.insertRoleReOpAcBy1(saveRoleReOpAc);

			return saveRoleReOpAc;
		}
		
		String[] updateProperties = com.fand.utils.BeanUtils.differProperties(saveRoleReOpAc, oldRoleReOpAc, "code", "name");

		if (ArrayUtils.isNotEmpty(updateProperties)) {
			this.roleDao.updateRoleReOpAcBy1(saveRoleReOpAc, updateProperties, whereEqProperties);

			com.fand.utils.BeanUtils.copyProperties(saveRoleReOpAc, oldRoleReOpAc, updateProperties);
		}

		return oldRoleReOpAc;
	}

	@Override
	@Transactional
	public RoleReOpAcDto saveRoleReOpAcBy2ForRoleReOpAc(RoleReOpAcDto saveRoleReOpAc) {
		String[] whereEqProperties = null;

//		if (StringUtils.isNotBlank(saveRoleReOpAc.getId())) {
//			whereEqProperties = new String[] { "id" };
//		} else {
//			whereEqProperties = new String[] { "code" };
//		}
	
		RoleReOpAcDto oldRoleReOpAc = this.roleDao.selectRoleReOpAcBy1ForMappedObject(RoleReOpAcDto.class,
				saveRoleReOpAc, null, whereEqProperties);

		if (oldRoleReOpAc == null) {
			this.roleDao.insertRoleReOpAcBy1(saveRoleReOpAc, new String[] {});

			return saveRoleReOpAc;
		}
		
		String[] updateProperties = com.fand.utils.BeanUtils.differProperties(saveRoleReOpAc, oldRoleReOpAc, "code", "name");

		if (ArrayUtils.isNotEmpty(updateProperties)) {
			this.roleDao.updateRoleReOpAcBy1(saveRoleReOpAc, updateProperties, whereEqProperties);

			com.fand.utils.BeanUtils.copyProperties(saveRoleReOpAc, oldRoleReOpAc, updateProperties);
		}

		return oldRoleReOpAc;
	}

	@Override
	@Transactional
	public int deleteRoleReOpAcBy1(RoleReOpAcDto deleteRoleReOpAc) {
		return this.roleDao.deleteRoleReOpAcBy1(deleteRoleReOpAc, new String[] {});
	}

	@Override
	@Transactional
	public int deleteRoleReOpAcByIds(RoleReOpAcDto deleteRoleReOpAc) {
		return this.roleDao.deleteRoleReOpAcByIds(deleteRoleReOpAc);
	}

	@Override
	@Transactional
	public int setRoleReOpAcBy1(RoleReOpAcDto setRoleReOpAc) {
		return this.roleDao.updateRoleReOpAcBy1(setRoleReOpAc, new String[] {}, new String[] {});
	}

	@Override
	@Transactional
	public int setRoleReOpAcBy2(RoleReOpAcDto setRoleReOpAc) {
		return this.roleDao.updateRoleReOpAcBy2(setRoleReOpAc);
	}

	@Override
	public Integer readRoleReOpAcByCodeNqIdForInteger(RoleReOpAcDto readRoleReOpAc) {
		return this.roleDao.selectRoleReOpAcByCodeNqIdForInteger(readRoleReOpAc);
	}

	@Override
	public Integer readRoleReOpAcBy2ForInteger(RoleReOpAcDto readRoleReOpAc) {
		return this.roleDao.selectRoleReOpAcBy2ForObject(Integer.class, readRoleReOpAc, null);
	}

	@Override
	public List<Integer> readListRoleReOpAcBy1(RoleReOpAcDto readRoleReOpAc) {
		return this.roleDao.selectRoleReOpAcBy1ForObjectList(Integer.class, readRoleReOpAc, null,
				new String[] {});
	}

	@Override
	public List<Integer> readListRoleReOpAcBy2(RoleReOpAcDto readRoleReOpAc) {
		return this.roleDao.selectRoleReOpAcBy2ForObjectList(Integer.class, readRoleReOpAc, null);
	}

	@Override
	public RoleReOpAcDto getRoleReOpAcBy1(RoleReOpAcDto getRoleReOpAc) {
		return this.roleDao.selectRoleReOpAcBy1ForMappedObject(RoleReOpAcDto.class, getRoleReOpAc, null, "id");
	}

	@Override
	public RoleReOpAcDto getRoleReOpAcById(RoleReOpAcDto getRoleReOpAc) {
		return this.roleDao.selectRoleReOpAcBy1ForMappedObject(RoleReOpAcDto.class, getRoleReOpAc, null, "id");
	}

	@Override
	public List<RoleReOpAcDto> findRoleReOpAcBy1(RoleReOpAcDto findRoleReOpAc) {
		return this.roleDao.selectRoleReOpAcBy1ForMappedObjectList(RoleReOpAcDto.class, findRoleReOpAc,
				new String[] {}, new String[] {});
	}

	@Override
	public List<RoleReOpAcDto> findRoleReOpAcBy2(RoleReOpAcDto findRoleReOpAc) {
		return this.roleDao.selectRoleReOpAcBy2ForMappedObjectList(RoleReOpAcDto.class, findRoleReOpAc);
	}

	@Override
	public PaginationDto<RoleReOpAcDto> paginationRoleReOpAcBy1ForRoleReOpAcList(RoleReOpAcDto paginationRoleReOpAc) {
		return this.roleDao.paginationRoleReOpAcBy1ForMappedObjectList(RoleReOpAcDto.class,
				paginationRoleReOpAc);
	}

	@Override
	public PaginationDto<Map<String, Object>> paginationRoleReOpAcBy1ForMapList(RoleReOpAcDto paginationRoleReOpAc) {
		return this.roleDao.paginationRoleReOpAcBy1ForMapList(paginationRoleReOpAc);
	}
}